#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   sim_cos_utils.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/22 0022 15:40   st      1.0         None
'''
import numpy as np


def similarity_cosine(vector1=None, vector2=None):  # 余弦夹角
    # similarity = 0
    try:
        cos1 = np.sum(vector1 * vector2)  # 矩阵求和
        cos21 = np.sqrt(sum(vector1 ** 2))  # 开根号
        cos22 = np.sqrt(sum(vector2 ** 2))
        if cos21 * cos22 > 0:
            similarity = cos1 / float(cos21 * cos22)
            return round(similarity, 2)
        else:
            return 0
    except:
        return 0